<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="/layui/css/layui.css">
</head>
<body>
<fieldset class="layui-elem-field" >
    <legend>修改密码</legend>
    <div class="layui-field-box" >
        <form class="layui-form">
            <div class="layui-form-item">
                <label class="layui-form-label">原密码</label>
                <div class="layui-input-inline">
                    <input type="password" id="oldpwd" name="oldpwd" required  lay-verify="required" placeholder="请输入密码" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">新密码</label>
                <div class="layui-input-inline">
                    <input type="password" id="newpwd" name="newpwd" required  lay-verify="required" placeholder="请输入密码" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">再次输入</label>
                <div class="layui-input-inline">
                    <input type="password" id="newpwd2"  required  lay-verify="required" placeholder="请输入密码" autocomplete="off" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit lay-filter="massageForm">立即提交</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </form>
    </div>
</fieldset>
<script type="text/javascript" src="/layui/layui.js"></script>
<script>
    //Demo
    // 待学生自主完成
    layui.use(['form','jquery','layer','laydate'], function(){
        var $ = layui.jquery;
        var form = layui.form();
        var layer = layui.layer;
        //监听提交
        form.on('submit(massageForm)', function(params){
            //表单数据
             var oldpwd = $("#oldpwd").val();
             var newpwd = $("#newpwd").val();
             var newpwd2 = $("#newpwd2").val();


            //等同于上面注释掉的部分
//            var data = $("form").serializeArray();

            // 封装之后的格式
            //var data = {"username":username,"password":password}
            if($("#newpwd").val()!=$("#newpwd2").val()){
                alert("密码不一致！")
            }else {
                $.ajax({
                    type: "POST",
                    url: "/user/pwdForm",  //后台程序地址
                    data: {"oldpwd":oldpwd,"newpwd":newpwd},  //需要post的数据
                    success: function(data){           //后台程序返回的标签，比如我喜欢使用1和0 表示成功或者失败
                        if (data.result == 'success'){   //如果成功了, 则关闭当前layer
                            layer.msg('修改成功',{
                                        icon: 1,
                                        time: 1000 //1秒关闭（如果不配置，默认是3秒）
                                    }
                                    ,function(){//
                                        //do something
                                        //注册成功后，自动关闭当前注册页面
                                        //先得到当前iframe层的索引
                                        var index = parent.layer.getFrameIndex(window.name);
                                        parent.layer.close(index);
                                        //parent.layer.closeAll("iframe");
                                    });
                        }else {
                            layer.msg('修改失败，原密码不正确',{
                                icon: 1,
                                time: 1000 //1秒关闭（如果不配置，默认是3秒）
                            })
                        }
                    }
                });
            }

            return false;//return false 表示不通过页面刷新方式提交表单
        });
    });
</script>
</body>
</html>